import request from '../../../../utils/request'
Page({
  data: {
    name:'',
    num:'',
    unit:'',
    isEnabled: true,
    selectedTime: '',
    timeList: '',
    reminderDays: [
      { id: 1, name: '每隔1天', value: 1 },
      { id: 2, name: '每隔2天', value: 2 },
      { id: 3, name: '每隔3天', value: 3 }
    ],
    units:[
      { id: 1, name: '片', value: 1 },
      { id: 2, name: '粒', value: 2 },
      { id: 3, name: '毫升', value: 3 },
      { id: 4, name: '克', value: 4 }],
    selectedReminderDay: null,
    showTimePicker: false,
    hour: '00',
    minute: '00',
    hours: Array.from({length: 24}, (_, i) => i < 10 ? '0' + i : '' + i),
    minutes: Array.from({length: 60}, (_, i) => i < 10 ? '0' + i : '' + i)
  },

  onTimeTap() {
    this.setData({
      showTimePicker: true
    });
  },

  cancelTime() {
    this.setData({
      showTimePicker: false
    });
  },

  confirmTime() {
    const { hour, minute } = this.data;
    const newTime = `${hour}:${minute}`;
    
    if (!this.data.timeList.includes(newTime)) {
      const newTimeList = newTime;
      this.setData({
        timeList: newTimeList,
        showTimePicker: false
      });
    }
  },

  bindTimeChange(e) {
    const values = e.detail.value;
    const hour = this.data.hours[values[0]];
    const minute = this.data.minutes[values[1]];
    
    this.setData({
      hour,
      minute
    });
  },

  deleteTime(e) {
    const { index } = e.currentTarget.dataset;
    const timeList = [...this.data.timeList];
    timeList.splice(index, 1);
    this.setData({
      timeList
    });
  },

  onReminderDaysTap() {
    // 处理提醒天数选择
    wx.showActionSheet({
      itemList: this.data.reminderDays.map(item => item.name),
      success: (res) => {
        this.setData({
          selectedReminderDay: this.data.reminderDays[res.tapIndex]
        });
      }
    });
  },

  onUnitTap() {
    // 处理单位选择
    wx.showActionSheet({
      itemList: this.data.units.map(item=>item.name),
      success: (res) => {
        // 处理单位选择结果
        this.setData({
          unit: this.data.units[res.tapIndex]
        });
      }
    });
  },

  onSwitchChange(e) {
    this.setData({
      isEnabled: e.detail.value
    });
  },
  bindname:function(e){
    this.setData({
      name:e.detail.value
    })
  },
  bindnum:function(e){
    this.setData({
      num:e.detail.value
    })
  },
  onSave() {
    // 处理保存逻辑
    request({
      url: 'tixing/tixing',
      method: "POST",
     data:{
      name:this.data.name,
      time:this.data.timeList,
      days:this.data.selectedReminderDay.value,
      dan:this.data.unit.name,
      num:this.data.num,
      isrun:1
     },
      success: (result) => {},
      fail: (err) => {},
      complete: (res) => {},
    })
    wx.showToast({
      title: '保存成功',
      icon: 'success'
    });
  }
});
